package com.controller;

import com.entity.Employee;
import com.service.EmployeeService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Data
@Controller
@ResponseBody
@RequestMapping(produces = "application/json;charset=utf-8")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    // 查询所有员工请求 http://localhost:8080/selectEmployeeAll
    @RequestMapping("/selectEmployeeAll")
    public List<Employee> selectEmployeeAll() {
        return employeeService.selectEmployeeAll();
    }

    // 用 name 查询员工请求 http://localhost:8080/selectEmployeeByName?name=刘
    @RequestMapping("/selectEmployeeByName")
    public Employee selectEmployeeByName(String name) {
        return employeeService.selectEmployeeByName(name);
    }

    // 用 id 查询员工请求 http://localhost:8080/selectEmployeeById?id=1001
    @RequestMapping("/selectEmployeeById")
    public Employee selectEmployeeById(int id) {
        return employeeService.selectEmployeeById(id);
    }

    // 用 id 删除员工请求 http://localhost:8080/deleteEmployeeById?id=1001
    @RequestMapping("/deleteEmployeeById")
    public String deleteEmployeeById(int id) {
        return employeeService.deleteEmployeeById(id) ? "删除成功" : "删除失败";
    }

    // 新增员工请求 http://localhost:8080/insertEmployee
    /**
         {
         "empno": 0,
         "ename": "陈俊杰",
         "job": "程序员",
         "mgr": 1006,
         "hiredate": "2001-02-20",
         "sal": 16000.0,
         "comm": 3000.0,
         "deptno": 20
         }
     */
    @RequestMapping("insertEmployee")
    public String insertEmployee(@RequestBody Employee employee) {
        return employeeService.insertEmployee(employee) ? "新增成功" : "新增失败";
    }

    // 修改员工请求 http://localhost:8080/updateEmployeeById
    /**
     {
     "empno": 1015,
     "ename": "陈俊杰",
     "job": "程序猿",
     "mgr": 1006,
     "hiredate": "2001-02-20",
     "sal": 16000.0,
     "comm": 3000.0,
     "deptno": 30
     }
     */
    @RequestMapping("updateEmployeeById")
    public String updateEmployeeById(@RequestBody Employee employee) {
        return employeeService.updateEmployeeById(employee) ? "修改成功" : "修改失败";
    }
}
